package com.twu.biblioteca.dao.memory;

import com.google.common.collect.Lists;

import com.twu.biblioteca.dao.Dao;
import com.twu.biblioteca.entity.Base;
import com.twu.biblioteca.exception.DaoNotFoundException;

import java.util.List;
import java.util.UUID;

public class MemoryBaseDao<T extends Base> implements Dao<T> {
  protected List<T> itemList = Lists.newArrayList();

  public void setItemList(List<T> itemList) {
    this.itemList = itemList;
  }

  @Override
  public List<T> findAll() {
    return itemList;
  }

  @Override
  public T find(UUID itemId) {
    for (T t : itemList) {
      if (t.getUuid().equals(itemId))
        return t;
    }
    throw new DaoNotFoundException("No Item found with the given ID");
  }

  @Override
  public void update(UUID id, T item) {
    T t = find(id);
    itemList.remove(t);
    itemList.add(item);
  }
}
